<template>
  <div class="addmoneyItem">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="报销人" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="报销类型" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="餐费"></el-radio>
          <el-radio label="打车费"></el-radio>
          <el-radio label="其他费用"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="报销时间" required>
        <el-col :span="22">
          <el-form-item prop="date">
            <!-- <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="ruleForm.date"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker> -->
            <el-date-picker v-model="ruleForm.date"  format="yyyy-MM-dd"
              style="width: 100%" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="报销金额" prop="money">
        <el-input v-model="ruleForm.money"></el-input>
      </el-form-item>
      <el-form-item label="报销说明" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即添加</el-button
        >
        <el-button @click="$router.go(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        money: 0,
        date: "",
        resource: "",
        desc: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入报销人名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        date: [
          {
            type: "date",
            required: true,
            message: "请选择报销日期",
            trigger: "change",
          },
        ],
        resource: [
          { required: true, message: "请选择报销类型", trigger: "change" },
        ],
        desc: [{ required: false, message: "请填写报销说明", trigger: "blur" }],
        money: [{ required: true, message: "请填写报销金额", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let ret = await this.$store.dispatch(
            "money/addMoneyAction",
            this.ruleForm
          );
          if(!ret.code){
            this.$message.success('添加成功！')
            this.$router.replace('/admin/money')
          }
        } else {
          this.$message.error('添加失败！')
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.addmoneyItem {
  width: 400px;
  min-height: 500px;
  margin-top: 50px;
  margin-left: 50px;
}
.ivu-btn-ghost {
  color: pink;
}
</style>